package space.panker916.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import space.panker916.pojo.User;
import space.panker916.service.UserService;

/**
 * @program: niit-spring
 * @description:
 * @author: PanKer
 * @create: 2022-04-18 22:35
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/index")
    public String toHome() {
        return "/index";
    }

    @GetMapping("/add")
    public String getAddPage() {
        return "/userRegister";
    }

    @PostMapping("/add")
    public String addUser(User user, Model model) {
        int rows = userService.insertUser(user);
        if (rows > 0) model.addAttribute("msg", "用户添加成功了！！！");
        else model.addAttribute("msg", "用户添加失败了！！！");
        return "/userRegister";
    }

    @GetMapping("/update")
    public String getUpdatePage() {
        return "/userUpdate";
    }

    @PostMapping("/update")
    public String updateUser(User user, Model model) {
        int rows = userService.updateUser(user);
        if (rows > 0) model.addAttribute("msg", "用户更新成功了！！！");
        else model.addAttribute("msg", "用户更新失败了！！！");
        return "/userUpdate";
    }
}
